// 让node能连接上mysql
const mysql = require('mysql')
const config = require('../config')

// 创建线程池
const pool = mysql.createPool({
  host: config.database.HOST,
  user: config.database.USERNAME,
  password: config.database.PASSWORD,
  database: config.database.DATABASE,
  port: config.database.PORT
})

// 连接数据库，执行sql语句
const allService = {
  query: function (sql, values) {
    return new Promise((resolve, reject) => {
      pool.getConnection((err, connection) => { // 连接数据库
        if (err) {
          reject(err)
        } else { // 连接成功
          connection.query(sql, values, (err, rows) => { // 执行sql语句
            if (err) {
              reject(err)
            } else { // sql语句执行成功
              resolve(rows)
            }
            connection.release() // 断开连接
          })
        }
      })
    })
  }
}

// live
const live = () => {
  let _sql = `select * from live`
  return allService.query(_sql)
}

// recommend
const recommend = () => {
  let _sql = `select * from recommend`
  return allService.query(_sql)
}

// shop
const shop = () => {
  let _sql = `select id,name,price,img1  from shop`
  return allService.query(_sql)
}

//show
const show = (value) => {
  let _sql = `select * from shop where id=${value}`
  return allService.query(_sql)
}

//search
const search = (value) => {
  let _sql = `select * from recommend where title like '%${value}%' `
  return allService.query(_sql)
}

const slideshow = () => {
  let _sql = `select * from slideshow`
  return allService.query(_sql)
}

//login
const login = (obj) =>{
  let _sql = `select * from user where phonenumber=${obj.account}`
  return allService.query(_sql)
}

//register
const register = (obj) =>{
  let _sql = `insert into user (phonenumber,password,name) values (${obj.number},${obj.password},${obj.name})`
  return allService.query(_sql)
}

//add
const add = (obj) =>{
  let 
}

  module.exports = {
    live,
    recommend,
    shop,
    search,
    slideshow,
    show,
    login,
    register,
    add
  }